package com.jndv.phoneconfig.fragment;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.jndv.phoneconfig.PublicConstant;
import com.jndv.phoneconfig.R;
import com.jndv.phoneconfig.command.GpsStateCommand;
import com.jndv.phoneconfig.command.MobileDataStateCommand;
import com.jndv.phoneconfig.command.MusicVolumeCommand;
import com.kyleduo.switchbutton.SwitchButton;
import com.warkiz.tickseekbar.OnSeekChangeListener;
import com.warkiz.tickseekbar.SeekParams;
import com.warkiz.tickseekbar.TickSeekBar;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

public class SystemFragment extends BaseFragment {

    SwitchButton sb;
    TickSeekBar seekBar;
    int maxVolume;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_system;
    }

    @Override
    protected void initView(View view) {
        sb = view.findViewById(R.id.sb);
        sb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d("zhang", "isChecked==" + isChecked);
                sendMessage(PublicConstant.GPS, isChecked ? "1" : "0");
            }
        });


        seekBar = view.findViewById(R.id.seekbar);

        seekBar.setOnSeekChangeListener(new OnSeekChangeListener() {
            @Override
            public void onSeeking(SeekParams seekParams) {

            }

            @Override
            public void onStartTrackingTouch(TickSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(TickSeekBar seekBar) {
                Log.d("zhang", seekBar.getProgress() + "");
                int curVolume = seekBar.getProgress() * maxVolume / 100;
                sendMessage(PublicConstant.MUSIC_VOLUME, curVolume + "");
            }
        });

        if(isSocketConnect()){
            sendMessage(PublicConstant.CUR_GPS_STATE, "");
            sendMessage(PublicConstant.CUR_MUSIC_VOLUME, "");
        }else {
            sb.setChecked(false);
            seekBar.setProgress(0);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGpsStateEvent(GpsStateCommand event) {
        sb.setChecked(event.isOn);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMusicVolumeStateEvent(MusicVolumeCommand event) {
        maxVolume = event.maxVolume;
        seekBar.setProgress(event.curVolume * 100 / event.maxVolume);
    }
}
